--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

-- Countdown Timer:
-- start: starting tick
-- delay: time until the timer expires
Countdown = {};

function Countdown.Create(_delay)
  local instance = { start = GetTick(), delay = _delay };

  --[[ Start the countdown ]]--
  -- We reset the start and checkpoint times.
  function instance:Start()
    self.start = GetTick();
  end

  --[[ Measure the time elapsed ]]--
  -- We return the number of milliseconds elapsed.
  function instance:MeasureElapsed()
    return GetTick() - self.start;
  end
  --[[ Measure the time remaining ]]--
  -- We return the number of milliseconds remaining. Negative means that
  -- the timer has expired.
  function instance:MeasureRemaining()
    return (self.start + self.delay) - GetTick();
  end

  --[[ Spawn a new countdown based on the time remaining ]]--
  -- We reset the start and checkpoint times.
  function instance:Mark()
    return Countdown.Create(self:MeasureRemaining());
  end

  return instance;
end
